﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using DevExpress.XtraReports.UI;
using ERP_RORZE_SYSTEM;
using System.Data;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class rpt_Inspection : DevExpress.XtraReports.UI.XtraReport
    {
        public string serialno_from;
        public string serialno_to;
        public string shippingdate;
        public string customer;
        public rpt_Inspection()
        {
            InitializeComponent();
            this.serialno_from = null;
            this.serialno_to = null;
            this.shippingdate = null;
            this.customer = null;
        }

        public void BindData()
        {
            sampleCode.DataBindings.Add("Text", DataSource, "SampleCode");
            shippingDate.Text = shippingdate;
            if (this.serialno_from == this.serialno_to)
            {
                serialNo.Text = this.serialno_from.Substring(0, 7);
            }
            else
            {
                serialNo.Text = this.serialno_from.Substring(0, 7) + " ~ " + this.serialno_to.Substring(0, 7);
            }
            partName.DataBindings.Add("Text", DataSource, "DCompName");
            partNo.DataBindings.Add("Text", DataSource, "CompID");            
            customerName.DataBindings.Add("Text", DataSource, "CustomerName");

            
        }

        public void LoadData()
        {
            DataSet sourceDataSet = new DataSet();
            INISettings mySettings = new INISettings();
            SQLManagement mySQLManagement = new SQLManagement();
            mySQLManagement.SQLConnectionString = mySettings.SQLConnectionString;
            string sql = "SELECT"
                + " CASE WHEN t1.CompID IN ('RV201-F05-009', 'RV201-F05-009-1', 'RV201-F05-014',"
                + " 'RR410L3802-0513-03-0', 'RR420L7001-1901-00-0', 'RR420L7002-1901-01-1', 'RR421L8002-1901-01-0',"
                + " 'RR421L8002-1901-01-1') THEN t1.Remark ELSE t1.[CompID] END AS [CompID], t2.[DCompName],"
                + " t3.[CustomerName], CASE t2.[Type] WHEN 'CL' THEN 'KGH-0026E-0' WHEN 'MT EFEM' THEN 'KWH-0707E-2'"
                + " ELSE 'KWH-0005E-0' END AS SampleCode"
                + " FROM [231_RobotOrderDetail] AS t1 LEFT JOIN"
                + " [231_Comp] AS t2 ON t1.[CompID] = t2.[CompID] LEFT JOIN"
                + " [231_Customer] AS t3 ON t3.[CustomerID] = '" + this.customer + "'"
                + " WHERE t1.[SerialNo] = '" + this.serialno_to + "'";
            sourceDataSet = mySQLManagement.SQL_Query(sql);
            if (sourceDataSet != null)
            {
                try
                {
                    this.DataSource = sourceDataSet.Tables[0];                    
                    BindData();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                MessageBox.Show("Cannot Load data to report");
            }
        }
    }
}
